package com.henu.acmrating.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.henu.acmrating.common.Result;
import com.henu.acmrating.pojo.ProblemWithUser;
import com.henu.acmrating.pojo.RankItem;
import com.henu.acmrating.service.ProblemService;
import com.henu.acmrating.service.UserOjService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/api")
public class ProblemController {

    @Resource
    ProblemService problemService;

    @Resource
    UserOjService userOjService;

    /**
     * 拿到动态记录
     */
    @RequestMapping(value = "/dynamic", method = RequestMethod.POST)
    public Result dynamic(@RequestBody JSONObject json) {
        Integer page = json.getInteger("page");
        String userName = json.getString("userName");
        PageInfo<ProblemWithUser> problemWithUserPageInfo;

        // 是否有搜索名字
        if (userName.length() != 0) {
            problemWithUserPageInfo = problemService.problemByUserNamewithPage(page, 20, userName);
            if (problemWithUserPageInfo.getTotal() == 0) {
                return Result.error("没有这个人的记录~");
            }
        } else {
            problemWithUserPageInfo = problemService.problemWithUserwithPage(page, 20);
        }
        return Result.success(problemWithUserPageInfo);
    }

    /**
     * 拿到排名
     */
    @RequestMapping(value = "showRank", method = RequestMethod.POST)
    public Result showRank(@RequestBody JSONObject json) {
        Integer page = json.getInteger("page");
        Integer days = json.getInteger("days");
        Boolean isRole = json.getBoolean("role");
        String userName = json.getString("userName");
        PageInfo<RankItem> rankItemPageInfo;

        // 是否有搜索名字
        if (userName.length() != 0) {
            rankItemPageInfo = problemService.showRankByUserwithPage(page, 20, days, userName);
            if (rankItemPageInfo.getTotal() == 0) {
                return Result.error("没有这个人~");
            }
        } else {
            // 0找现役 2找全部队员
            int role = isRole ? 2 : 0;
            if(days == 36500) {
                rankItemPageInfo = userOjService.showRankwithPage(page, 20, role);
            } else {
                rankItemPageInfo = problemService.showRankwithPage(page, 20, days, role);
            }
        }
        return Result.success(rankItemPageInfo);
    }


    /**
     * 登陆用户 一天、一周、一个月的刷题情况
     */
    @RequestMapping(value = "mySolved", method = RequestMethod.GET)
    public Result mySolved(@RequestParam("id") Integer userId) {
        RankItem day = problemService.showRankByUserId(1, userId);
        RankItem week = problemService.showRankByUserId(7, userId);
        RankItem month = problemService.showRankByUserId(30, userId);
        JSONObject json = new JSONObject();
        json.put("day", day == null ? 0 : day.getSolved());
        json.put("week", week == null ? 0 : week.getSolved());
        json.put("month", month == null ? 0 : month.getSolved());
        return Result.success(json);
    }
}

